/**
 * SpringMVC restful 风格
 * 订单管理控制器，控制关于订单的一些业务逻辑操作
 * @author yanwl
 * @date 2014-10-08
 */
package com.chnbst.bstyun.controller.user;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.chnbst.bstyun.core.common.CreateOrderNum;
import com.chnbst.bstyun.core.common.DateUtil;
import com.chnbst.bstyun.core.common.PriceUtil;
import com.chnbst.bstyun.core.common.ReadProperties;
import com.chnbst.bstyun.core.common.SignUtil;
import com.chnbst.bstyun.eos.CreateNum;
import com.chnbst.bstyun.eos.Eos;
import com.chnbst.bstyun.model.pojo.Cashflow;
import com.chnbst.bstyun.model.pojo.Deal;
import com.chnbst.bstyun.model.pojo.Order;
import com.chnbst.bstyun.model.pojo.User;
import com.chnbst.bstyun.model.vo.MealFeeVo;
import com.chnbst.bstyun.model.vo.MealProductVo;
import com.chnbst.bstyun.model.vo.OrderVo;
import com.chnbst.bstyun.model.vo.ProductFeeVo;
import com.chnbst.bstyun.service.CashflowService;
import com.chnbst.bstyun.service.DealService;
import com.chnbst.bstyun.service.MealFeeService;
import com.chnbst.bstyun.service.MealProductService;
import com.chnbst.bstyun.service.MealService;
import com.chnbst.bstyun.service.OrderService;
import com.chnbst.bstyun.service.ProductFeeService;
import com.chnbst.bstyun.service.ProductService;
import com.chnbst.bstyun.service.UserService;
import com.chnbst.bstyun.service.VoucherService;

@Controller
@RequestMapping("/myservice")
public class MyServiceController {
	/**
	 * 每页显示的订单条数
	 */
	//private int orderPageSize = 5;
	
	/**
	 * 注入业务逻辑层
	 */
	@Autowired
	private OrderService orderService;
	@Autowired
	private MealService mealService;
	@Autowired
	private ProductService productService;
	@Autowired
	private MealProductService mealProductService;
	@Autowired
	private UserService userService;
	@Autowired
	private ProductFeeService productFeeService;
	@Autowired
	private MealFeeService mealFeeService;
	@Autowired
	private VoucherService voucherService;
	@Autowired
	private DealService dealService;
	@Autowired
	private CashflowService cashflowService;
	@Autowired
	private CreateNum createNum;
	@Autowired
	private Eos eos;
	@Autowired
	private ReadProperties readProperties;
	
	@RequestMapping(value="/order/upgradeManage",method=RequestMethod.GET)
	public ModelAndView upgradeManage(HttpServletRequest request){
		ModelAndView mav = new ModelAndView();
		
		mav.setViewName("users/myservice/upgrade_manage");
		return mav;
	}
	
	@RequestMapping(value="/order/renewManage",method=RequestMethod.GET)
	public ModelAndView renewManage(HttpServletRequest request){
		ModelAndView mav = new ModelAndView();
		
		mav.setViewName("users/myservice/renew_manage");
		return mav;
	}
	
	/**
	 * ajax请求 查询用户购买过的订单信息
	 * @return 订单信息
	 */
	@RequestMapping(value="/order/upgradeManage/queryList",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> upgradeList(HttpServletRequest request){
		Map<String, Object> map = new HashMap<String, Object>();
		User user = (User)request.getSession().getAttribute("userInfo");
		map.put("userId", user.getUserId());
		map.put("overTime", null);
		map.put("sortField", "pay_time");
		map.put("sortOrder", "desc");
		map.put("groupField", "t.user_id,t.meal_id,t.new_pro_id");
		List<OrderVo> orders = orderService.selectLastOrdersByUserId(map);
		map.put("orders", orders);
		return map;
	}
	
	/**
	 * ajax请求 查询用户购买过的订单信息
	 * @return 订单信息
	 */
	@RequestMapping(value="/order/renewManage/queryList",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> renewList(String overTime,HttpServletRequest request){
		Map<String, Object> map = new HashMap<String, Object>();
		User user = (User)request.getSession().getAttribute("userInfo");
		map.put("userId", user.getUserId());
		map.put("overTime", overTime);
		map.put("sortField", "pay_time");
		map.put("sortOrder", "desc");
		map.put("groupField", "t.user_id,t.meal_id,t.new_pro_id");
		List<OrderVo> orders = orderService.selectLastOrdersByUserId(map);
		map.put("orders", orders);
		return map;
	}
	
	/**
	 * ajax请求 检测订单是否可以升级
	 * @return 订单信息
	 */
	@RequestMapping(value="/order/checkOrderUpgrade",method=RequestMethod.POST)
	@ResponseBody
	public boolean checkOrderUpgrade(String orderId,HttpServletRequest request){
		boolean isSuccess = true;
		Order entity = orderService.selectOrderByOrderId(Integer.valueOf(orderId));
		if(entity.getNewProId() != null) {
			//根据套餐Id和用户Id查找最近一条订单记录
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("mealId", entity.getMealId());
			map.put("userId", entity.getUserId());
			map.put("sortField", "pay_time");
			map.put("sortOrder", "desc");
			
			Order order = orderService.selectOrderByMealIdAndProductId(map);
			
			String dataStr = entity.getDataExtend();
			String[] str = dataStr.split("&");
			Integer userNum = Integer.valueOf(str[1]);
			int maxUserNum = order.getUserNum();
			
			if(userNum >= maxUserNum) {
				isSuccess = false;
			}
		}
		return isSuccess;
	}
	
	/**
	 * ajax请求 检测订单是否可以续费
	 * @return 订单信息
	 */
	@RequestMapping(value="/order/checkOrderRenew",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> checkOrderRenew(String orderId,HttpServletRequest request){
		Map<String, Object> resultMap = new HashMap<String, Object>();
		boolean isSuccess = true;
		Order order = orderService.selectOrderByOrderId(Integer.valueOf(orderId));
		if(order.getNewProId() != null) {
			//根据套餐Id和用户Id查找最近一条订单记录
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("mealId", order.getMealId());
			map.put("userId", order.getUserId());
			map.put("sortField", "pay_time");
			map.put("sortOrder", "desc");
			
			Order entity = orderService.selectOrderByMealIdAndProductId(map);
			
			long overTime = order.getProOverTime().getTime();
			long maxOverTime = entity.getOverTime().getTime();
			
			if(overTime >= maxOverTime) {
				isSuccess = false;
				resultMap.put("str", "当前使用时间已达到上线，如需继续续费，请先续费该产品加入的套餐！");
			}
		}else {
			if("-1".equals(order.getUseLife())) {
				isSuccess = false;
				resultMap.put("str", "永久性套餐不能续费！");
			}
		}
		
		resultMap.put("isSuccess", isSuccess);
		return resultMap;
	}
	
	
	/**
	 * http://xxx.com/myservice/order/toUpOrderPage
	 * get请求 升级用户
	 * @return 用户升级页面
	 */
	@RequestMapping(value="/order/toUpgradePage",method=RequestMethod.GET)
	public ModelAndView toUpgradePage(HttpServletRequest request){
		ModelAndView mav = new ModelAndView();
		
		Integer orderId = Integer.valueOf(request.getParameter("orderId"));
		OrderVo orderVo = orderService.selectOrderVoByOrderId(orderId);

		OrderVo order = new OrderVo();
		
		if(orderVo.getNewProId() != null) {
			//根据套餐Id和用户Id查找最近一条订单记录
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("mealId", orderVo.getMealId());
			map.put("userId", orderVo.getUserId());
			map.put("sortField", "pay_time");
			map.put("sortOrder", "desc");
			
			Order entity = orderService.selectOrderByMealIdAndProductId(map);
			orderVo.setUserNum(entity.getUserNum());
			
			ProductFeeVo mpf = productFeeService.selectProductFeeVoByProductId(orderVo.getNewProId());
			order.setMealName(mpf.getProductName());//产品名称
			
			String dataStr = orderVo.getDataExtend();
			String[] str = dataStr.split("&");
			order.setUseLife(str[0]);
			order.setUserNum(Integer.valueOf(str[1]));//用户数
			order.setOrderMoney(mpf.getProductFeePrice()/mpf.getUserNum()/mpf.getUseYear());//产品单价
			order.setMealId(mpf.getUseYear());//产品的基本年限
			mav.addObject("mpf", mpf);
		}else {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("mealId", orderVo.getMealId());
			map.put("start", 0);
			map.put("pageSize", Integer.MAX_VALUE);
			map.put("sortField", "mp.add_time");
			map.put("sortOrder", "asc");
			
			List<MealProductVo> mpv = mealProductService.selectMealProductVos(map);
			String products = "";
			for (int i = 0; i < mpv.size(); i++) {
				products += mpv.get(i).getProductName()+"*1";
				if(i < mpv.size()-1) {
					products += "+";
				}
			}
			
			order.setProductName(products);//套餐产品
			order.setUseLife(orderVo.getUseLife());
			order.setUserNum(orderVo.getUserNum());//用户数
			MealFeeVo mpf = mealFeeService.selectMealFeeVoByMealId(orderVo.getMealId());
			
			order.setMealName(mpf.getMealName());//套餐名称
			order.setMealId(mpf.getUserYear());//套餐的基本年限
			
			//单价计算
			double price = 0;
			if(mpf.getUserYear() == -1) {
				price = mpf.getPrice()/mpf.getUserNum();
			}else {
				price = mpf.getPrice()/mpf.getUserNum()/mpf.getUserYear();
			}
			order.setOrderMoney(price);//产品单价
			mav.addObject("mpf", mpf);
		}
		mav.addObject("orderVo", orderVo);
		mav.addObject("order", order);
		mav.setViewName("users/myservice/upgrade_order");
		return mav;
	}
	
	/**
	 * http://xxx.com/myservice/order/toRenewPage
	 * get请求 升级用户
	 * @return 用户升级页面
	 */
	@RequestMapping(value="/order/toRenewPage",method=RequestMethod.GET)
	public ModelAndView toRenewPage(HttpServletRequest request){
		ModelAndView mav = new ModelAndView();
		
		Integer orderId = Integer.valueOf(request.getParameter("orderId"));
		OrderVo orderVo = orderService.selectOrderVoByOrderId(orderId);
		
		OrderVo order = new OrderVo();
		
		//根据套餐Id和用户Id查找最近一条订单记录
		Map<String, Object> map1 = new HashMap<String, Object>();
		map1.put("mealId", orderVo.getMealId());
		map1.put("userId", orderVo.getUserId());
		map1.put("sortField", "pay_time");
		map1.put("sortOrder", "desc");
		
		Order entity = orderService.selectOrderByMealIdAndProductId(map1);
		orderVo.setOverTime(entity.getOverTime());
		
		if(orderVo.getNewProId() != null) {
			ProductFeeVo pf = productFeeService.selectProductFeeVoByProductId(orderVo.getNewProId());
			order.setMealName(pf.getProductName());//产品名称
			
			String dataStr = orderVo.getDataExtend();
			String[] str = dataStr.split("&");
			order.setUseLife(str[0]);
			order.setUserNum(Integer.valueOf(str[1]));//用户数
			order.setOrderMoney(pf.getProductFeePrice()/pf.getUserNum()/pf.getUseYear());//产品单价
		}else {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("mealId", orderVo.getMealId());
			map.put("start", 0);
			map.put("pageSize", Integer.MAX_VALUE);
			map.put("sortField", "mp.add_time");
			map.put("sortOrder", "asc");
			
			List<MealProductVo> mpv = mealProductService.selectMealProductVos(map);
			String products = "";
			for (int i = 0; i < mpv.size(); i++) {
				products += mpv.get(i).getProductName()+"*1";
				if(i < mpv.size()-1) {
					products += "+";
				}
			}
			
			order.setProductName(products);//套餐产品
			order.setUseLife(orderVo.getUseLife());
			order.setUserNum(orderVo.getUserNum());//用户数
			MealFeeVo mf = mealFeeService.selectMealFeeVoByMealId(orderVo.getMealId());
			
			order.setMealName(mf.getMealName());//套餐名称
			
			//单价计算
			double price = 0;
			if(mf.getUserYear() == -1) {
				price = mf.getPrice()/mf.getUserNum();
			}else {
				price = mf.getPrice()/mf.getUserNum()/mf.getUserYear();
			}
			order.setOrderMoney(price);//套餐单价
		}
		
		mav.addObject("orderVo", orderVo);
		mav.addObject("order", order);
		mav.setViewName("users/myservice/renew_order");
		return mav;
	}
	
	/**
	 * http://xxx.com/myservice/order/toUpgradePayPage
	 * get请求 升级用户
	 * @return 用户升级页面
	 * @throws Exception 
	 */
	@RequestMapping(value="/order/toUpgradePayPage",method=RequestMethod.GET)
	public ModelAndView toUpgradePayPage(HttpServletRequest request) throws Exception{
		ModelAndView mav = new ModelAndView();
		Integer orderId = Integer.valueOf(request.getParameter("orderId"));
		Integer userNum = Integer.valueOf(request.getParameter("userNum"));
		
		Order order = orderService.selectOrderByOrderId(orderId);
		
		//查找该账户下最新的订单
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("mealId", order.getMealId());
		map.put("userId", order.getUserId());
		map.put("sortField", "pay_time");
		map.put("sortOrder", "desc");
		Order lastOrder = orderService.selectOrderByMealIdAndProductId(map);
		
		int currUserNum = 0;
		int userNumSpace = 0;
		int newUserNum = lastOrder.getUserNum();
		User user = (User)request.getSession().getAttribute("userInfo");
		String orderNum = CreateOrderNum.getOrderNum(user.getUserId());
		Order entity = new Order();
		int month = 0;
		double orderMoney = 0.00;
		
		String proMealName = "";
		String EOSresult = "";
		String productId = "0";
		if(order.getNewProId() != null) {
			String dataStr = order.getDataExtend();
			String[] str = dataStr.split("&");
			currUserNum = Integer.valueOf(str[1]);
			userNumSpace = userNum - currUserNum;
			ProductFeeVo pf = productFeeService.selectProductFeeVoByProductId(order.getNewProId());
			long overTime = SignUtil.str2Date(str[2], "yyyy-MM-dd").getTime();
			if(overTime >= order.getOverTime().getTime()) {
				month = Integer.valueOf(DateUtil.getMonth("getMonth", new Date(),order.getOverTime()));
			}else {
				month = Integer.valueOf(DateUtil.getMonth("getMonth", new Date(),SignUtil.str2Date(str[2], "yyyy-MM-dd")));
			}
			orderMoney = PriceUtil.getPrice(pf.getProductFeePrice(), pf.getUseYear(), pf.getUserNum(), month, userNumSpace);
			entity.setNewProId(order.getNewProId());
			entity.setDataExtend(str[0]+"&"+userNum+"&"+str[2]);
			proMealName = pf.getProductName();
			productId = String.valueOf(order.getNewProId());
		}else {
			currUserNum = order.getUserNum();
			userNumSpace = userNum - currUserNum;
			newUserNum = userNum;
			MealFeeVo mf = mealFeeService.selectMealFeeVoByMealId(order.getMealId());
			month = Integer.valueOf(DateUtil.getMonth("getMonth", new Date(),order.getOverTime()));
			orderMoney = PriceUtil.getPrice(mf.getPrice(), mf.getUserYear(), mf.getUserNum(), month, userNumSpace);
			proMealName = mf.getMealName();
		}
		entity.setProOverTime(order.getProOverTime());
		if(orderMoney == 0) {
			entity.setBuyType(2);
			entity.setCount(1);
			entity.setIsDredge(0);
			entity.setIsFree(0);
			entity.setMealId(order.getMealId());
			entity.setOccurTime(new Date());
			entity.setOrderMoney(orderMoney);
			entity.setOrderNum(orderNum);
			entity.setOrderState("0");
			entity.setOverTime(order.getOverTime());
			entity.setParentId(order.getParentId());
			entity.setProIds(lastOrder.getProIds());
			entity.setUseLife(order.getUseLife());
			entity.setUserId(order.getUserId());
			entity.setUserNum(newUserNum);
			entity.setPayByAccount(0.00);
			entity.setPayByOther(0.00);
			entity.setPayMoney(0.00);
			entity.setPayTime(new Date());
			entity.setPayWayId(2);
			
			try {
				orderService.insertOrder(entity);
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			//=============================新增一条交易记录开始======================================
			Deal deal = new Deal();
			deal.setDealDate(new Date());
			deal.setDealName(proMealName);
			deal.setDealNum(orderNum);
			deal.setDealState("2");
			deal.setDisburseMoney(order.getOrderMoney());
			deal.setIncomeMoney(0.0);
			deal.setPayWayId(2);
			deal.setUserId(order.getUserId());
			deal.setParentId(order.getParentId());
			try {
				dealService.insertDeal(deal);
			} catch (Exception e) {
				e.printStackTrace();
			}
			//=============================新增一条交易记录结束======================================
			
			//=============================收支明细增加开始======================================
			//交易收支明细
			Cashflow cashflow = new Cashflow();
			cashflow.setCashflowNum(createNum.createCashflowNum("2"));
			cashflow.setCreateTime(new Date());
			cashflow.setDealType(2);
			cashflow.setDisburseMoney(0.00);
			cashflow.setIncomeMoney(0.00);
			cashflow.setOrderId(entity.getOrderId());
			cashflow.setPayWayId(2);
			cashflow.setThenAccount(user.getAccountMoney());
			cashflow.setUserId(user.getUserId());
			try {
				cashflowService.insertCashflow(cashflow);
			} catch (Exception e) {
				e.printStackTrace();
			}
			//=============================收支明细增加结束======================================
			
			//=============================普元数据同步开始======================================
			if(entity.getMealId() != 3) {
				EOSresult = eos.sendProOrMeal(entity.getOrderId());
			}
			if("ok".equals(EOSresult)) {
				//更改订单的开通状态
				Order updateOrder = orderService.selectOrderByOrderId(entity.getOrderId());
				updateOrder.setIsDredge(1);//0失败  1成功
				orderService.updateOrderByOrderId(updateOrder);
			}
			//=============================普元数据同步开始======================================
			
			
			mav.setViewName("users/myservice/uporders_success");
		}else {
			entity.setBuyType(2);
			entity.setCount(1);
			entity.setIsDredge(0);
			entity.setIsFree(0);
			entity.setMealId(order.getMealId());
			entity.setOccurTime(new Date());
			entity.setOrderMoney(orderMoney);
			entity.setOrderNum(orderNum);
			entity.setOrderState("1");
			entity.setOverTime(order.getOverTime());
			entity.setParentId(order.getParentId());
			entity.setProIds(lastOrder.getProIds());
			entity.setUseLife(order.getUseLife());
			entity.setUserId(order.getUserId());
			entity.setUserNum(newUserNum);
			entity.setPayByAccount(0.00);
			entity.setPayByOther(0.00);
			entity.setPayMoney(0.00);
			
			try {
				orderService.insertOrder(entity);
			} catch (Exception e) {
				e.printStackTrace();
			}
			mav.setViewName("users/myservice/uporders_pay");
		}
		
		request.getSession().setAttribute("order", entity);
		request.getSession().setAttribute("productId", productId);
		request.getSession().setAttribute("userInfo", user);
		request.getSession().setAttribute("proMealName", proMealName);
		request.getSession().setAttribute("EOSresult", EOSresult);
		request.getSession().setAttribute("BSTCOMPANYINFO", readProperties);
		return mav;
	}
	
	/**
	 * http://xxx.com/myservice/order/toRenewPayPage
	 * get请求 产品续费
	 * @return 产品续费页面
	 * @throws Exception 
	 */
	@RequestMapping(value="/order/toRenewPayPage",method=RequestMethod.GET)
	public ModelAndView toRenewPayPage(HttpServletRequest request) throws Exception{
		ModelAndView mav = new ModelAndView();
		Integer orderId = Integer.valueOf(request.getParameter("orderId"));
		Integer year = Integer.valueOf(request.getParameter("year"));
		Order order = orderService.selectOrderByOrderId(orderId);
		
		//查找该账户下最新的订单
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("mealId", order.getMealId());
		map.put("userId", order.getUserId());
		map.put("sortField", "pay_time");
		map.put("sortOrder", "desc");
		Order lastOrder = orderService.selectOrderByMealIdAndProductId(map);
		
		int currUserLife = Integer.valueOf(lastOrder.getUseLife());
		Date currOverTime = lastOrder.getOverTime();
		User user = (User)request.getSession().getAttribute("userInfo");
		String orderNum = CreateOrderNum.getOrderNum(user.getUserId());
		Order entity = new Order();
		int month = 0;
		double orderMoney = 0.00;
		
		String proMealName = "";
		String EOSresult = "";
		String productId = "0";
		if(order.getNewProId() != null) {
			String dataStr = order.getDataExtend();
			String[] str = dataStr.split("&");
			int userNum = Integer.valueOf(str[1]);
			String proUserLife = str[0];
			Date proOverTime =  SignUtil.str2Date(str[2], "yyyy-MM-dd");
			Date proNewOverTime = SignUtil.getOvertime(1, +year,proOverTime);
			
			ProductFeeVo pf = productFeeService.selectProductFeeVoByProductId(order.getNewProId());
			if(proNewOverTime.getTime() >= lastOrder.getOverTime().getTime()) {
				proNewOverTime = lastOrder.getOverTime();
				month = Integer.valueOf(DateUtil.getMonth("getMonth", proOverTime,lastOrder.getOverTime()));
			}else {
				month = year*12;
			}
			proUserLife = DateUtil.getMonth("getOverTime", proOverTime,proNewOverTime);
			orderMoney = PriceUtil.getPrice(pf.getProductFeePrice(), pf.getUseYear(), pf.getUserNum(), month, userNum);
			entity.setNewProId(order.getNewProId());
			entity.setDataExtend(proUserLife+"&"+userNum+"&"+SignUtil.time2str(proNewOverTime, "yyyy-MM-dd"));
			entity.setProOverTime(proNewOverTime);
			proMealName = pf.getProductName();
			productId = String.valueOf(order.getNewProId());
		}else {
			currUserLife += year;
			currOverTime = SignUtil.getOvertime(1, +year,currOverTime);
			
			MealFeeVo mf = mealFeeService.selectMealFeeVoByMealId(order.getMealId());
			month = year*12;
			orderMoney = PriceUtil.getPrice(mf.getPrice(), mf.getUserYear(), mf.getUserNum(), month, order.getUserNum());
			proMealName = mf.getMealName();
			entity.setProOverTime(currOverTime);
		}
		if(orderMoney == 0) {
			entity.setBuyType(1);
			entity.setCount(1);
			entity.setIsDredge(0);
			entity.setIsFree(0);
			entity.setMealId(order.getMealId());
			entity.setOccurTime(new Date());
			entity.setOrderMoney(orderMoney);
			entity.setOrderNum(orderNum);
			entity.setOrderState("0");
			entity.setOverTime(currOverTime);
			entity.setParentId(order.getParentId());
			entity.setProIds(lastOrder.getProIds());
			entity.setUseLife(String.valueOf(currUserLife));
			entity.setUserId(order.getUserId());
			entity.setUserNum(order.getUserNum());
			entity.setPayByAccount(0.00);
			entity.setPayByOther(0.00);
			entity.setPayMoney(0.00);
			entity.setPayTime(new Date());
			entity.setPayWayId(2);
			
			try {
				orderService.insertOrder(entity);
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			//=============================新增一条交易记录开始======================================
			Deal deal = new Deal();
			deal.setDealDate(new Date());
			deal.setDealName(proMealName);
			deal.setDealNum(orderNum);
			deal.setDealState("2");
			deal.setDisburseMoney(order.getOrderMoney());
			deal.setIncomeMoney(0.0);
			deal.setPayWayId(2);
			deal.setUserId(order.getUserId());
			deal.setParentId(order.getParentId());
			try {
				dealService.insertDeal(deal);
			} catch (Exception e) {
				e.printStackTrace();
			}
			//=============================新增一条交易记录结束======================================
			
			//=============================收支明细增加开始======================================
			//交易收支明细
			Cashflow cashflow = new Cashflow();
			cashflow.setCashflowNum(createNum.createCashflowNum("2"));
			cashflow.setCreateTime(new Date());
			cashflow.setDealType(2);
			cashflow.setDisburseMoney(0.00);
			cashflow.setIncomeMoney(0.00);
			cashflow.setOrderId(entity.getOrderId());
			cashflow.setPayWayId(2);
			cashflow.setThenAccount(user.getAccountMoney());
			cashflow.setUserId(user.getUserId());
			try {
				cashflowService.insertCashflow(cashflow);
			} catch (Exception e) {
				e.printStackTrace();
			}
			//=============================收支明细增加结束======================================
			
			//=============================普元数据同步开始======================================
			if(entity.getMealId() != 3) {
				EOSresult = eos.sendProOrMeal(entity.getOrderId());
			}
			if("ok".equals(EOSresult)) {
				//更改订单的开通状态
				Order updateOrder = orderService.selectOrderByOrderId(entity.getOrderId());
				updateOrder.setIsDredge(1);//0失败  1成功
				orderService.updateOrderByOrderId(updateOrder);
			}
			//=============================普元数据同步结束======================================
			mav.setViewName("users/myservice/uporders_success");
		}else {
			entity.setBuyType(1);
			entity.setCount(1);
			entity.setIsDredge(0);
			entity.setIsFree(0);
			entity.setMealId(order.getMealId());
			entity.setOccurTime(new Date());
			entity.setOrderMoney(orderMoney);
			entity.setOrderNum(orderNum);
			entity.setOrderState("1");
			entity.setOverTime(currOverTime);
			entity.setParentId(order.getParentId());
			entity.setProIds(lastOrder.getProIds());
			entity.setUseLife(String.valueOf(currUserLife));
			entity.setUserId(order.getUserId());
			entity.setUserNum(order.getUserNum());
			entity.setPayByAccount(0.00);
			entity.setPayByOther(0.00);
			entity.setPayMoney(0.00);
			
			try {
				orderService.insertOrder(entity);
			} catch (Exception e) {
				e.printStackTrace();
			}
			mav.setViewName("users/myservice/uporders_pay");
		}
		
		request.getSession().setAttribute("order", entity);
		request.getSession().setAttribute("productId", productId);
		request.getSession().setAttribute("userInfo", user);
		request.getSession().setAttribute("proMealName", proMealName);
		request.getSession().setAttribute("EOSresult", EOSresult);
		request.getSession().setAttribute("BSTCOMPANYINFO", readProperties);
		return mav;
	}
}
